home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / AudioVideo / Player / AudioVideoPlayer.cs next >
Encoding:
Text File  |  2004-09-27  |  11.9 KB  |  329 lines

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using Microsoft.DirectX;
  7. using Microsoft.DirectX.AudioVideoPlayback;
  8. using Microsoft.Samples.DirectX.UtilityToolkit;
  9.  
  10. namespace Player
  11. {
  12.     /// <summary>
  13.     /// Summary description for Form1.
  14.     /// </summary>
  15.     public class AVPlayer : System.Windows.Forms.Form
  16.     {
  17.  
  18.         private string filterText = "Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv)|*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv|" +
  19.             "Audio files (*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma)|*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma|" +
  20.             "MIDI Files (*.mid, *.midi, *.rmi)|*.mid; *.midi; *.rmi|" +
  21.             "Image Files (*.jpg, *.bmp, *.gif, *.tga)|*.jpg; *.bmp; *.gif; *.tga|" +
  22.             "All Files (*.*)|*.*";
  23.  
  24.         private Video videoClip = null;
  25.         private Audio audioClip = null;
  26.  
  27.         #region Winforms variables
  28.  
  29.         /// <summary>
  30.         /// Required designer variable.
  31.         /// </summary>
  32.         private System.ComponentModel.Container components = null;
  33.         private System.Windows.Forms.MainMenu mnuMain;
  34.         private System.Windows.Forms.OpenFileDialog ofdOpen;
  35.         private System.Windows.Forms.MenuItem menuItem3;
  36.         private System.Windows.Forms.MenuItem mnuFile;
  37.         private System.Windows.Forms.MenuItem mnuOpen;
  38.         private System.Windows.Forms.MenuItem menuItem1;
  39.         private System.Windows.Forms.MenuItem mnuPlay;
  40.         private System.Windows.Forms.MenuItem mnuStop;
  41.         private System.Windows.Forms.MenuItem mnuPause;
  42.         private System.Windows.Forms.MenuItem menuItem2;
  43.         private System.Windows.Forms.MenuItem mnuFull;
  44.         private System.Windows.Forms.MenuItem mnuExit;
  45.         #endregion
  46.  
  47.         /// <summary>Create the object and call the open file method</summary>
  48.         public AVPlayer()
  49.         {
  50.             InitializeComponent();
  51.             OpenFile();
  52.         }
  53.  
  54.         #region Windows Form Designer generated code
  55.         /// <summary>
  56.         /// Clean up any resources being used.
  57.         /// </summary>
  58.         protected override void Dispose( bool disposing )
  59.         {
  60.             CleanupObjects();
  61.             if( disposing )
  62.             {
  63.                 if (components != null) 
  64.                 {
  65.                     components.Dispose();
  66.                 }
  67.             }
  68.             base.Dispose( disposing );
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Required method for Designer support - do not modify
  73.         /// the contents of this method with the code editor.
  74.         /// </summary>
  75.         private void InitializeComponent()
  76.         {
  77.             this.mnuMain = new System.Windows.Forms.MainMenu();
  78.             this.mnuFile = new System.Windows.Forms.MenuItem();
  79.             this.mnuOpen = new System.Windows.Forms.MenuItem();
  80.             this.menuItem3 = new System.Windows.Forms.MenuItem();
  81.             this.mnuExit = new System.Windows.Forms.MenuItem();
  82.             this.menuItem1 = new System.Windows.Forms.MenuItem();
  83.             this.mnuPlay = new System.Windows.Forms.MenuItem();
  84.             this.mnuStop = new System.Windows.Forms.MenuItem();
  85.             this.mnuPause = new System.Windows.Forms.MenuItem();
  86.             this.ofdOpen = new System.Windows.Forms.OpenFileDialog();
  87.             this.menuItem2 = new System.Windows.Forms.MenuItem();
  88.             this.mnuFull = new System.Windows.Forms.MenuItem();
  89.             // 
  90.             // mnuMain
  91.             // 
  92.             this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  93.                                                                                     this.mnuFile,
  94.                                                                                     this.menuItem1});
  95.             // 
  96.             // mnuFile
  97.             // 
  98.             this.mnuFile.Index = 0;
  99.             this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  100.                                                                                     this.mnuOpen,
  101.                                                                                     this.menuItem3,
  102.                                                                                     this.mnuExit});
  103.             this.mnuFile.Text = "&File";
  104.             // 
  105.             // mnuOpen
  106.             // 
  107.             this.mnuOpen.Index = 0;
  108.             this.mnuOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
  109.             this.mnuOpen.Text = "&Open Clip";
  110.             this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
  111.             // 
  112.             // menuItem3
  113.             // 
  114.             this.menuItem3.Index = 1;
  115.             this.menuItem3.Text = "-";
  116.             // 
  117.             // mnuExit
  118.             // 
  119.             this.mnuExit.Index = 2;
  120.             this.mnuExit.Text = "E&xit";
  121.             this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
  122.             // 
  123.             // menuItem1
  124.             // 
  125.             this.menuItem1.Index = 1;
  126.             this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  127.                                                                                       this.mnuPlay,
  128.                                                                                       this.mnuStop,
  129.                                                                                       this.mnuPause,
  130.                                                                                       this.menuItem2,
  131.                                                                                       this.mnuFull});
  132.             this.menuItem1.Text = "&Control";
  133.             // 
  134.             // mnuPlay
  135.             // 
  136.             this.mnuPlay.Index = 0;
  137.             this.mnuPlay.Shortcut = System.Windows.Forms.Shortcut.CtrlP;
  138.             this.mnuPlay.Text = "&Play";
  139.             this.mnuPlay.Click += new System.EventHandler(this.mnuPlay_Click);
  140.             // 
  141.             // mnuStop
  142.             // 
  143.             this.mnuStop.Index = 1;
  144.             this.mnuStop.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
  145.             this.mnuStop.Text = "&Stop";
  146.             this.mnuStop.Click += new System.EventHandler(this.mnuStop_Click);
  147.             // 
  148.             // mnuPause
  149.             // 
  150.             this.mnuPause.Index = 2;
  151.             this.mnuPause.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
  152.             this.mnuPause.Text = "P&ause";
  153.             this.mnuPause.Click += new System.EventHandler(this.mnuPause_Click);
  154.             // 
  155.             // menuItem2
  156.             // 
  157.             this.menuItem2.Index = 3;
  158.             this.menuItem2.Text = "-";
  159.             // 
  160.             // mnuFull
  161.             // 
  162.             this.mnuFull.Index = 4;
  163.             this.mnuFull.Text = "Toggle Fu&llscreen\t<Alt-Enter>";
  164.             this.mnuFull.Click += new System.EventHandler(this.mnuFull_Click);
  165.             // 
  166.             // AVPlayer
  167.             // 
  168.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  169.             this.ClientSize = new System.Drawing.Size(264, 38);
  170.             this.Menu = this.mnuMain;
  171.             this.Name = "AVPlayer";
  172.             this.Text = "Audio Video Player";
  173.  
  174.         }
  175.         #endregion
  176.  
  177.         /// <summary>Cleans up resources used by the player</summary>
  178.         private void CleanupObjects()
  179.         {
  180.             if (videoClip != null)
  181.             {
  182.                 videoClip.Stop();
  183.                 videoClip.Dispose();
  184.             }
  185.             videoClip = null;
  186.  
  187.             if (audioClip != null)
  188.             {
  189.                 audioClip.Stop();
  190.                 audioClip.Dispose();
  191.             }
  192.             audioClip = null;
  193.         }
  194.  
  195.         private void OpenFile()
  196.         {
  197.             if ((ofdOpen.InitialDirectory == null) || (ofdOpen.InitialDirectory == string.Empty))
  198.                 ofdOpen.InitialDirectory = Utility.SdkMediaPath;
  199.  
  200.             ofdOpen.Filter = filterText;
  201.             ofdOpen.Title = "Open media file";
  202.             ofdOpen.CheckFileExists = true;
  203.  
  204.             if (ofdOpen.ShowDialog(this) == DialogResult.OK)
  205.             {
  206.                 // Cleanup previous
  207.                 CleanupObjects();
  208.  
  209.                 // Now let's try to open this file
  210.                 try
  211.                 {
  212.                     // First try to open this as a video file
  213.                     videoClip = new Video(ofdOpen.FileName);
  214.                     videoClip.Ending += new System.EventHandler(this.ClipEnded);
  215.                     videoClip.Owner = this;
  216.                     // Start playing now
  217.                     videoClip.Play();
  218.                 }
  219.                 catch
  220.                 {
  221.                     try
  222.                     {
  223.                         // opening this as a video file failed.. Maybe it's audio only?
  224.                         audioClip = new Audio(ofdOpen.FileName);
  225.                         audioClip.Ending += new System.EventHandler(this.ClipEnded);
  226.                         // Start playing now
  227.                         audioClip.Play();
  228.                     }
  229.                     catch
  230.                     {
  231.                         MessageBox.Show("This file could not be opened.", "Invalid file.", MessageBoxButtons.OK, MessageBoxIcon.Information);
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.  
  237.         /// <summary>
  238.         /// The main entry point for the application.
  239.         /// </summary>
  240.         static void Main() 
  241.         {
  242.             using (AVPlayer player = new AVPlayer())
  243.             {
  244.                 Application.Run(player);
  245.             }
  246.         }
  247.  
  248.         /// <summary>
  249.         /// Fired when the audio or video has ended
  250.         /// </summary>
  251.         private void ClipEnded(object sender, System.EventArgs e)
  252.         {
  253.             // The clip has ended, stop and restart it
  254.             if (videoClip != null)
  255.             {
  256.                 videoClip.Stop();
  257.                 videoClip.Play();
  258.             }
  259.             else
  260.             {
  261.                 if (audioClip != null)
  262.                 {
  263.                     audioClip.Stop();
  264.                     audioClip.Play();
  265.                 }
  266.             }
  267.         }
  268.  
  269.         private void mnuOpen_Click(object sender, System.EventArgs e)
  270.         {
  271.             this.OpenFile();
  272.         }
  273.  
  274.         private void mnuPlay_Click(object sender, System.EventArgs e)
  275.         {
  276.             if (videoClip != null)
  277.                 videoClip.Play();
  278.             else
  279.             {
  280.                 if (audioClip != null)
  281.                     audioClip.Play();
  282.             }
  283.         }
  284.  
  285.         private void mnuStop_Click(object sender, System.EventArgs e)
  286.         {
  287.             if (videoClip != null)
  288.                 videoClip.Stop();
  289.             else
  290.             {
  291.                 if (audioClip != null)
  292.                     audioClip.Stop();
  293.             }
  294.         }
  295.  
  296.         private void mnuPause_Click(object sender, System.EventArgs e)
  297.         {
  298.             if (videoClip != null)
  299.                 videoClip.Pause();
  300.             else
  301.             {
  302.                 if (audioClip != null)
  303.                     audioClip.Pause();
  304.             }
  305.         }
  306.  
  307.         private void mnuExit_Click(object sender, System.EventArgs e)
  308.         {
  309.             this.Dispose();
  310.         }
  311.  
  312.         private void mnuFull_Click(object sender, System.EventArgs e)
  313.         {
  314.             if (videoClip != null)
  315.                 videoClip.Fullscreen = !videoClip.Fullscreen;
  316.         }
  317.         protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
  318.         {
  319.             if ( (e.Alt) && (e.KeyCode == System.Windows.Forms.Keys.Return))
  320.             {
  321.                 mnuFull_Click(mnuFull, null);
  322.             }
  323.  
  324.             // Allow the control to handle the keystroke now
  325.             base.OnKeyDown(e);
  326.         }
  327.     }
  328. }
  329.